package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;


@RestController
@RequestMapping("/vilidataCode")
public class ValidateCodeController {


    @Autowired
    private JedisPool jedisPool;
//    发送手机验证码
/*
* 预约发送验证码
* */
    @RequestMapping("/send4Code")
    public Result send4Order(String telephone){
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        try{
            System.out.println(code);
            System.out.println(telephone);
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code);
//            int i=1/0;
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL,",服务器正忙，请稍后重试");
        }
//        如果测试通过把验证码存储在redis中，并返回验证吗发送成功的消息
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code);
         return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /*
    * 登录验证码发送
    * */
    @RequestMapping("/send6Login")
    public Result send6Login(String telephone){
        String code6 = ValidateCodeUtils.generateValidateCode(6).toString();
        try{
            System.out.println(code6);
            System.out.println(telephone);

//            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,code6);

        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL,",服务器正忙，请稍后重试");
        }
//        如果测试通过把验证码存储在redis中，并返回验证吗发送成功的消息
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,code6);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
